name: Sync code formatting
on:
  push:
    branches:
      - master

jobs:
  autofix-markdown:
    name: Autofix Markdown files using Prettier
    runs-on: ubuntu-latest
    steps:
      - name: Harden Runner
        uses: step-security/harden-runner@63c24ba6bd7ba022e95695ff85de572c04a18142 # v2.7.0
        with:
          egress-policy: audit

      - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
        with:
          # Fetch changes to previous commit - required for 'only_changed' in Prettier action
          fetch-depth: 0

      - name: use node.js ${{ matrix.node-version }}
        uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
        with:
          node-version: ${{ matrix.node-version }}
          registry-url: https://registry.npmjs.org/ # Needed for auth
      - name: yarn install
        uses: backstage/actions/yarn-install@a674369920067381b450d398b27df7039b7ef635 # v0.6.5
        with:
          cache-prefix: ${{ runner.os }}-v${{ matrix.node-version }}

      - name: Run Prettier on ADOPTERS.md
        uses: creyD/prettier_action@31355f8eef017f8aeba2e0bc09d8502b13dbbad1 # v4.3
        with:
          # Modifies commit only if prettier autofixed the ADOPTERS.md
          prettier_options: --config docs/prettier.config.js --write ADOPTERS.md
          only_changed: true
          commit_message: 'Autofix ADOPTERS.md using Prettier'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
